package entity;

import java.util.Random;

import map.Map;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Vector2f;

import animation.MacAnimation;
import constans.Global;
import constans.Parametr;
import constans.ResourceManager;
import frame.Frame;

public class Hero extends MovingEntity{
	
	private HerosCannon cannon; 
	//stats
	float attackSpeed = Parametr.HAttackSpeed;
	float attackAccuracy = Parametr.HAttackAccuracy;
    
	Sound[] movingSnd;
	Sound[] dmgSnd;
	
	public Hero(Vector2f position) {
		super(position);
		size = 40.f;
		speed = Parametr.HSpeed;
		damage = Parametr.HDamage;
		maxLife = Parametr.HMaxLife;
		actLife = maxLife;
		regenerationSpeed = Parametr.HRegenerationSpeed;
		cannon = new HerosCannon(position);
		
		animation = new MacAnimation(ResourceManager.getImage("robot"), 100, size);
		
		movingSnd = new Sound[4];
		movingSnd[0] = ResourceManager.getSound("m1");
		movingSnd[1] = ResourceManager.getSound("m2");
		movingSnd[2] = ResourceManager.getSound("m3");
		movingSnd[3] = ResourceManager.getSound("m4");
		
		dmgSnd = new Sound[3];
		dmgSnd[0] = ResourceManager.getSound("d1");
		dmgSnd[1] = ResourceManager.getSound("d2");
		dmgSnd[2] = ResourceManager.getSound("d3");
		
		Map.shift.add(position);
		target.set(position);
	}
	public float getAttackSpeed() {
		return attackSpeed;
	}
	public float getAttackAccuracy() {
		return attackAccuracy;
	}
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		position.sub(Map.shift);
		animation.draw(position.x - size/2, position.y - size/2);
		position.add(Map.shift);
		
		cannon.render(container, g);
	}
	public void update(GameContainer container, int delta) throws SlickException
	{		
		Input input = container.getInput();
		
		Vector2f mousePosition = new Vector2f(input.getMouseX(), input.getMouseY());
		if(container.getInput().isMouseButtonDown(1))
			if(!Frame.isOn(mousePosition))
				if(mousePosition.distance(position) > 30)
				{
					Vector2f relativeMousePosition = mousePosition.add(Map.shift);			
					target.set(calculate(relativeMousePosition));
					boolean sn = true;
					for(Sound s : movingSnd)
						if(s.playing())
						{
							sn = false;
							break;
						}
					if(sn)
						if(Math.random() > 0.97)
							movingSnd[new Random().nextInt(movingSnd.length)].play();
				}
					
         
		super.update(container, delta);
		
		if(target.distance(position) > speed)
		{
			animation.setDirection(movement);
			animation.update();
			move();
			Map.shift.add(movement);
		}
		cannon.update(damage, attackSpeed, attackAccuracy);
		cannon.update(container, delta);
		lifeRegeneration();
	}
	private Vector2f calculate(Vector2f r) {
		if(r.x < Map.margin)
			r.x = Map.margin;
		if(r.x > Global.MAP_SIZE_X - Map.margin)
			r.x = Global.MAP_SIZE_X - Map.margin;
		if(r.y < Map.margin)
			r.y = Map.margin;
		if(r.y > Global.MAP_SIZE_Y - Map.margin)
			r.y = Global.MAP_SIZE_Y - Map.margin;
		return r;
	
	}
	public Bullet shoot()
	{
		cannon.shoot = false;
		return cannon.shoot();
	}
	public boolean isBullet() {
		
		return cannon.shoot;
	}
	public void addAttackSpeed()
	{
		attackSpeed += Parametr.HAttackSpeedDX;
	}
	public void addAttackAccuracy()
	{
		attackAccuracy += Parametr.HAttackAccuracyDX;
	}
	public void addDamage()
	{
		damage += Parametr.HDmageDX;
	}
	public void addSpeed()
	{
			speed += Parametr.HSpeedDX;
	}
	public void addMaxLife()
	{
		maxLife += Parametr.HMaxLifeDX;
	}
	public void takeDamage(float d)
	{
		super.takeDamage(d);
		boolean sn = true;
		for(Sound s : dmgSnd)
			if(s.playing())
			{
				sn = false;
				break;
			}
		if(sn)
				dmgSnd[new Random().nextInt(dmgSnd.length)].play();
	}
}
